//app.js
var httpUtil = require('utils/httpUtil.js')
var uiUtil = require('utils/uiUtil.js')
var that
const STORAGE_CARTSELECTED = "cartSelectedGoods"
App({
  globalData: {
    loginStatus: 0, //登录状态，0：正在登录，1:登录失败，2：登录成功
    loginCode: null,
    
    identity:null, //会员类型，1.正式会员 2.初级体验会员 3.高级体验会员

    // //正在进行中的数量
    cartCount: 0, //购物车数量
    // noTakeCount: 0, //订单数量
  },

  onLaunch: function() {
    that = this;
    //检查更新
    that.checkUpgrade();
    // 登录
    wx.login({
      success: res => {
        that.loginRequest(res.code);
      }
    })
  },
  /**
   * 登录请求
   */
  loginRequest(code) {
    httpUtil.PostRequest("user_login", {
      wxLoginCode: code
    }, {
      success(res) {
        that.globalData.loginCode = res.loginId
        that.globalData.identity = res.identity
        that.globalData.loginStatus = 2
      },
      fail(code, res) {
        that.globalData.loginStatus = 1
      },
      complete() {
        setTimeout(function() {
          if (that.loginRequestReadyCallback) {
            that.loginRequestReadyCallback()
          }
        }, 800)
      }
    })
  },

  /**
   * 重置购物车数量
   */
  resetCartCount(count) {
    that.globalData.cartCount = count
  },
  // /**
  //  * 更新（增加+减少）购物车数量
  //  */
  // updateCartCount(changeCount) {
  //   var lastCount = that.globalData.cartCount
  //   lastCount = lastCount + changeCount
  //   if (lastCount < 0) {
  //     lastCount = 0
  //   }
  //   that.globalData.cartCount = lastCount
  // },
  // /**
  //  * 重置待收货数量
  //  */
  // resetNoTakeCount(count) {
  //   that.globalData.noTakeCount = count
  // },
  // /**
  //  * 更新（增加+减少）待收货数量
  //  */
  // updateNoTakeCount(changeCount) {
  //   var lastCount = that.globalData.noTakeCount
  //   lastCount = lastCount + changeCount
  //   if (lastCount < 0) {
  //     lastCount = 0
  //   }
  //   that.globalData.noTakeCount = lastCount
  // },


  //====购物车已选中商品列表========
  /**
   * 存储key:cartSelectedGoods
   * 存储value:[goodId&skuId,.....]
   * 
   * 
   */
  /**
   * 重置
   */
  cartSelectsReset(dataList) {
    wx.setStorage({
      key: STORAGE_CARTSELECTED,
      data: dataList,
    })
  },
  /**
   * 获取已选择购物车商品列表
   */
  cartSelectsGet() {
    var list = wx.getStorageSync(STORAGE_CARTSELECTED) || []
    return list;
  },
  /**
   * 拼装数据
   */
  cartSelectsPkData(goodId, skuId) {
    return goodId + '&' + skuId
  },

  /**
   * 新增已选择商品
   */
  cartSelectsAdd(goodId, skuId) {
    var data = this.cartSelectsPkData(goodId, skuId)
    var list = this.cartSelectsGet()
    if (list.indexOf(data) == -1) { //原数据无此商品
      list.unshift(data);
      this.cartSelectsReset(list)
    }
  },
  /**
   * 删除已选择商品
   */
  cartSelectsDel(goodId, skuId) {
    var data = this.cartSelectsPkData(goodId, skuId)
    var list = this.cartSelectsGet()
    var index = list.indexOf(data)
    if (index != -1) {
      list.splice(index, 1)
      this.cartSelectsReset(list)
    }
  },

  /**
   * 查询省钱信息
   */
  orderSaveMoneyInfo(orderId,fun){
    httpUtil.PostRequest("order_save_money_info", {
      orderId: orderId
    }, {
      success(res) {
        let text = "省下的才是纯利，恭喜您下单成功，本笔订单帮您节省了" + res.saveAmount+"元。"
        res.text=text;
        fun(res);
      },
      fail(code, res) {
        uiUtil.showToast(res);
      }
    })
  },



  /**
   * 检测升级
   */
  checkUpgrade() {
    const updateManager = wx.getUpdateManager()
    updateManager.onCheckForUpdate(function(res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        uiUtil.showToast("发现新版本，即将更新后重启")
        updateManager.onUpdateReady(function() {
          // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
          updateManager.applyUpdate()
        })
        updateManager.onUpdateFailed(function() {
          // 新的版本下载失败
          wx.showModal({
            title: '已经有新版本了哟~',
            content: '新版本已经上线啦~，请您删除当前小程序，重新搜索打开哟~',
          })
        })
      }
    })
  }
})